<template>
	<div class="tab-content">
		<view class="uni-common-mt">
			<uni-segmented-control :current="current" :values="tabBars" :style-type="styleType"
				:active-color="activeColor" @clickItem="onClickItem" />
		</view>
		<view v-show="current === 0">
			<view class="form_list">
				<view class="label">单号</view>
				<view class="input2">
					<billno prestr='QOUT' ref="billno" ></billno>
				</view>
			</view>
			<view class="form_list">
				<view class="label">单据日期</view>
				<view class="input">
					<uni-datetime-picker type="date" name="FDate" v-model="FDateVal" class="border" />
				</view>
			</view>
			<view class="form_list">
				<view class="label">摘要</view>
				<view class="input">
					<uni-easyinput type="textarea" placeholderStyle="font-size:17px" autoHeight v-model="FNote" placeholder="请输入内容"></uni-easyinput>
				</view>
			</view>
			
		</view>
		<view v-show="current === 1">
			<scroll-view scroll-y="true" style="height: 80vh;">
			<scanMaterial></scanMaterial>
			<view class="form_list">
				<view class="label">出库数量</view>
				<view class="input2">
					<uni-easyinput trim="all" type="number" v-model="FOperQty" placeholderStyle="font-size:17px"></uni-easyinput>
				</view>
			</view>
			
			<view class="swiper-button">
				<button @click="list">列表</button>
				<button @click="addOrder">完成</button>
				<button @click="toAddMaterialCheck">添加</button>
			</view>
			</scroll-view>
		</view>
		<view v-show="current === 2">
			<materialList :dataArr="outArr"></materialList>
		</view>
	</div>
</template>

<script>
	// 引入uni-ui组件
	import scanMaterial from '@/component/scan-code-material.vue'
	import billno from '@/component/billno.vue'
	import materialList from '@/component/show-material-list.vue'
	import {
		uniEasyinput,uniSegmentedControl,uniDatetimePicker
	} from '@dcloudio/uni-ui'
	export default {
		components: {
			uniEasyinput,scanMaterial,billno,materialList,
			uniSegmentedControl,uniDatetimePicker
		},
		data() {
			return {
				current: 1,
				/* 选中标签栏的序列,默认显示第一个 */
				tabBars: ['表头', '扫码', '明细'],
				activeColor: '#18bc9c',
				styleType: 'button',
				changeFlag:false,
				material:{},//物料明细
				FOperQty:'',//出库数量 操作数量
				FBillNo:'',//流水单号
				outArr:[],//所有要出库的物料数组
				FDateVal:this.$Func.getDateTime('today'), //单据日期
				FNote:''//摘要
			}
		},
		computed: {},
		watch:{},
		onLoad(options) { //调用方法并赋值
			var _this = this
			uni.$off('scanMaterial') // 每次进来先 移除全局自定义事件监听器  
			uni.$on('scanMaterial',function(material){  
				_this.material = material
			})
			//监听单号
			uni.$off('autoFBillNo')
			uni.$on('autoFBillNo',function(FBillNo){
				_this.FBillNo = FBillNo
			})
			//半秒后，更新单号
			setTimeout(()=>{
				this.$refs.billno.getCode()
			},500)
		},
		methods: {
			//选择弹窗
			list() {
				uni.navigateTo({
					url: '/pages/otheroutstock/list'
				})
			},
			//验证数量
			toAddMaterialCheck(){
				let _this = this
				if(JSON.stringify(this.material) == "{}"){
					this.$Api.msg('请先扫描','error');
					return ;
				} 
				this.FOperQty = parseFloat(this.FOperQty)
				if(isNaN(this.FOperQty)){
					this.$Api.msg('请输入出库数量','error')
					return ;
				}else{
					if(this.FOperQty<=0){
						this.$Api.msg('出库数量异常','error')
						return ;
					}
					this.material.FOperQty = this.FOperQty
					if(this.FOperQty>this.material.FStockQty){
						uni.showModal({
							title: '温馨提醒',
							content: '出库数量已超账面数量，是否继续？',
							success: function (res) {
								if (res.confirm) {
									_this.toAddMaterial()
								}
							}
						});
					}else{
						this.toAddMaterial()
					}
				}
			},
			//添加物料
			async toAddMaterial() {
				let clearFlag = false
				if(this.outArr.length>0){
					let repeatFlag = false
					for(let i=0;i<this.outArr.length;i++){
						let temp= this.outArr[i]
						if(temp.FItemID==this.material.FItemID&&temp.FBatchNo==this.material.FBatchNo&&temp.FAuxPropNumber==this.material.FAuxPropNumber){
							this.$Api.msg('已存在该箱头物料')
							repeatFlag = true
							return
						}
					}
					if(!repeatFlag){
						clearFlag = true
						this.outArr.push(this.material)
					}
				}else{
					clearFlag = true
					this.outArr.push(this.material)
				}
				if(clearFlag){
					await this.clearItem()
				}
			},
			clearItem() {
				//清除物料
				uni.$emit("clearScanMaterial")
				this.FOperQty = ''
				this.material = {}
			},
			clearBill(){
				this.$refs.billno.getCode()
				this.FNote = ''
				this.outArr = []
			},
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
			},
			async addOrder(){
				//提交其他出库单
				let _this = this;
				if(this.outArr.length>0){
					let bill = {
						'FBillNo':this.FBillNo,
						'FNote':this.FNote,
						'FDateVal':this.FDateVal
					}
					uni.showLoading({
						mask:true
					})
					this.$Db.addOrder(4,bill,this.outArr,{}).then(res=>{
						this.$Api.msg('操作成功','success')
						this.clearItem()
						this.clearBill()
						uni.hideLoading()
					}).catch(err=>{
						this.$Api.msg(err,'error')
						uni.hideLoading()
					})
				}else{
					this.$Api.msg('请先扫描','error')
				}
			}
		}
	}
</script>
<style lang="scss">
</style>